package com.kiragame.xchat.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.kiragame.xchat.R;
import com.kiragame.xchat.adapter.ChatRoomAdapter;
import com.kiragame.xchat.object.MessageContent;
import com.kiragame.xchat.service.ChatService;
import com.quentindommerc.superlistview.SuperListview;

public class ContactsFragment extends Fragment {

	private static final String ARG_POSITION = "position";

	private int position;

	private SuperListview lvChatRoom;
	private LinearLayout llSendMessage;
	private EditText etMessage;
	private Button btSend;
	private List<MessageContent> messageContentData = new ArrayList<MessageContent>();
	private ChatRoomAdapter chatAdapter;

	public static ContactsFragment newInstance() {
		ContactsFragment f = new ContactsFragment();
		// Bundle b = new Bundle();
		// b.putInt(ARG_POSITION, position);
		// f.setArguments(b);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//position = getArguments().getInt(ARG_POSITION);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_room, container, false);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		chatAdapter = new ChatRoomAdapter(getActivity(), messageContentData);
		lvChatRoom.setAdapter(chatAdapter);
	}

	private void setEvent() {
		btSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String message = etMessage.getText().toString();
//				ChatService.getInstance()
//						.sendMessageChatRoom(message);
			}
		});
	}

	private void findViewIds(View view) {
		lvChatRoom = (SuperListview) view.findViewById(R.id.lv_chat_room);
		llSendMessage = (LinearLayout) view.findViewById(R.id.ll_send_message);
		etMessage = (EditText) view.findViewById(R.id.et_message);
		btSend = (Button) view.findViewById(R.id.bt_send);
	}
	
	protected void updateListMessage(String message) {
		if (message != null) {
			messageContentData.add(MessageContent
					.fromJsonToMessageContent(message));
		}
		chatAdapter.notifyDataSetChanged();
	}

}
